home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / fftscop4.zip / FT-SCOPE.C < prev    next >
C/C++ Source or Header  |  1994-05-30  |  29KB  |  1,261 lines

  1. #include <dos.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <alloc.h>
  6. #include <string.h>
  7. #include <mem.h>
  8. #include <math.h>
  9. #define    TWO_PI    ((double)2.0 * M_PI)
  10.  
  11. typedef unsigned char DacPalette256[256][3];
  12. unsigned char far *VGAmem;
  13. struct text_info tiGFX;
  14. struct REGPACK regp;
  15. union  REGS reg;
  16. int Linecolor=1,WritePage=0,HighSpeed=0,Triggerbuffer=999,winX=1;
  17. int minX=0,minY=0,maxx=320,maxy=200;
  18.  
  19. DacPalette256 Hallo;
  20.  
  21. /* Globals */
  22.  
  23. int    samples, power;
  24. double    real[2048], imag[2048], max, min, Pegel=10,FFTmax;
  25. FILE    *fpi, *fpo;
  26.  
  27. /* Prototypes and forward declarations */
  28.  
  29. void    fft(void), max_amp(void);
  30. int    permute(int);
  31. double    magnitude(int);
  32. void dspwrite ( unsigned char );
  33. unsigned char dspread ( void );
  34. unsigned Buffer[2048];
  35. unsigned SBuffer[8900];
  36. unsigned char far *data;
  37. unsigned char far *aligned;
  38. unsigned char far aligned_physical;
  39.  
  40. /* Setvgapalette256 sets the entire 256 color palette */
  41. /* PalBuf contains RGB values for all 256 colors      */
  42. /* R,G,B values range from 0 to 63                  */
  43. /* Usage:                          */
  44. /*  DacPalette256 dac256;                  */
  45. /*                              */
  46. /* setvgapalette256(&dac256);                  */
  47. void setvgapalette256(DacPalette256 *PalBuf)
  48. {
  49.   regp.r_ax = 0x1012;
  50.   regp.r_bx = 0;
  51.   regp.r_cx = 256;
  52.   regp.r_es = FP_SEG(PalBuf);
  53.   regp.r_dx = FP_OFF(PalBuf);
  54.   intr(0x10,®p);
  55. }
  56.  
  57.  
  58.  
  59. void Grafik(int GFXmode)  // 13,      14,     17mono,   18,      19vga,  -1,3
  60. {                         //320x200, 640x200, 640x480, 640x480, 320x200, text
  61. VGAmem=(char far *)MK_FP(0xa000,0);
  62. if (GFXmode>=0)
  63.   {
  64.    gettextinfo(&tiGFX);
  65.    regp.r_ax = GFXmode;
  66.    intr(0x10,®p);
  67.   }
  68. if (GFXmode<0)
  69.   {
  70.    regp.r_ax = tiGFX.currmode;
  71.    intr(0x10,®p);
  72.   }
  73. }
  74.  
  75.  
  76.  
  77. void Putpixel(int x, int y)
  78. {
  79.  reg.h.ah = 0x0c;
  80.  reg.h.al = Linecolor;
  81.  reg.h.bh = WritePage;
  82.  reg.x.cx = x;
  83.  reg.x.dx = y;
  84.  int86(0x10,®,®);
  85. }
  86.  
  87.  
  88.  
  89. void singlecolor256(int nr,int r,int g,int b)
  90. {
  91.  reg.h.ah = 0x10;
  92.  reg.h.al = 0x10;
  93.  reg.x.bx = nr;
  94.  reg.h.ch = g;
  95.  reg.h.cl = b;
  96.  reg.h.dh = r;
  97.  int86(0x10,®,®);
  98. }
  99.  
  100.  
  101.  
  102. void singlecolor16(int nr,int r,int g,int b)
  103. {
  104.  reg.h.ah=0x10;
  105.  reg.h.al=0x07;
  106.  reg.h.bl=nr;
  107.  int86(0x10,®,®);
  108.  nr=reg.h.bh;
  109.  reg.h.ah = 0x10;
  110.  reg.h.al = 0x10;
  111.  reg.x.bx = nr;
  112.  reg.h.ch = g;
  113.  reg.h.cl = b;
  114.  reg.h.dh = r;
  115.  int86(0x10,®,®);
  116. }
  117.  
  118.  
  119.  
  120.  
  121. void MakePalette()
  122. {
  123.  int frag;
  124.  for(frag=0;frag<256;frag++)
  125.    {
  126.     if(frag<32)
  127.            {
  128.         Hallo[frag][0]=0;
  129.         Hallo[frag][1]=0;
  130.         Hallo[frag][2]=2*frag;
  131.            }
  132.     if((frag>=32)&&(frag<96))
  133.                  {
  134.                   Hallo[frag][0]=0;
  135.                   Hallo[frag][1]=(frag-32);
  136.                   Hallo[frag][2]=(95-frag);
  137.                  }
  138.     if((frag>=96)&&(frag<128))
  139.                   {
  140.                    Hallo[frag][0]=2*(frag-96);
  141.                    Hallo[frag][1]=63;
  142.                    Hallo[frag][2]=0;
  143.                   }
  144.     if((frag>=128)&&(frag<192))
  145.                    {
  146.                 Hallo[frag][0]=63;
  147.                 Hallo[frag][1]=(191-frag);
  148.                 Hallo[frag][2]=(frag-128);
  149.                    }
  150.     if(frag>=192)
  151.                 {
  152.               Hallo[frag][0]=63;
  153.           Hallo[frag][1]=0;
  154.           Hallo[frag][2]=0;
  155.           if(frag<223)Hallo[frag][2]=2*(222-frag);
  156.              }
  157.    }
  158.  setvgapalette256(&Hallo);
  159. }
  160.  
  161.  
  162. //------------------------------------------------------------------------------
  163. void dspwrite ( unsigned char c )
  164. {
  165.     while(inportb(0x022C)&0x80);
  166.     outportb(0x022C,c);
  167. }
  168. //------------------------------------------------------------------------------
  169. void sbinit ( void )
  170. {
  171.     unsigned short x;
  172.     inportb(0x022E);
  173.     outportb(0x0226,0x01);
  174.     inportb(0x0226);
  175.     inportb(0x0226);
  176.     inportb(0x0226);
  177.     inportb(0x0226);
  178.     outportb(0x0226,0x00);
  179.     for(x=0;x<100;x++)
  180.     {
  181.         if(inportb(0x022E)&0x80)
  182.         {
  183.             if(inportb(0x022A)==0xAA) break;
  184.         }
  185.     }
  186.     //if(x==100)
  187.     //{
  188.     //    printf("Sound Blaster not found at 0220h\n");
  189.     //    exit(1);
  190.     //}
  191. }
  192. //------------------------------------------------------------------------------
  193. void sbmalloc ( void )
  194. {
  195.     unsigned long physical;
  196.     data=farmalloc(80000L);
  197.     if(data==NULL)
  198.     {
  199.         printf("Memory Allocation Error\n");
  200.         exit(1);
  201.     }
  202.     physical=((unsigned long)FP_OFF(data))+(((unsigned long)FP_SEG(data))<<4);
  203.     physical+=0x0FFFFL;
  204.     physical&=0xF0000L;
  205.     aligned_physical=(physical>>16)&15;
  206.     aligned=MK_FP(((unsigned short)aligned_physical<<12)&0xF000,0);
  207. }
  208. //------------------------------------------------------------------------------
  209. void sbsettc ( unsigned char tc )
  210. // tc = time constant = 256L - (1000000UL/samples per second)
  211. {
  212.     inportb(0x022E);
  213.     dspwrite(0x40);
  214.     dspwrite(tc);
  215. }
  216. //------------------------------------------------------------------------------
  217. void sbrec ( unsigned short len )
  218. // len = number of bytes to record to unsigned char *aligned (<=65000)
  219. {
  220.     len--;
  221.     outportb(0x0A,0x05);
  222.     outportb(0x0C,0x00);
  223.     outportb(0x0B,0x45);
  224.     outportb(0x02,0);
  225.     outportb(0x02,0);
  226.     outportb(0x83,aligned_physical);
  227.     outportb(0x03,(unsigned char)(len&0xFF));
  228.     outportb(0x03,(unsigned char)((len>>8)&0xFF));
  229.     outportb(0x0A,0x01);
  230.     dspwrite(0x24);
  231.     dspwrite((unsigned char)(len&0xFF));
  232.     dspwrite((unsigned char)((len>>8)&0xFF));
  233. }
  234. //------------------------------------------------------------------------------
  235. void sbrec2 ( unsigned short len )
  236. // len = number of bytes to record to unsigned char *aligned (<=65000)
  237. {
  238.     len--;
  239.     outportb(0x0A,0x05);
  240.     outportb(0x0C,0x00);
  241.     outportb(0x0B,0x45);
  242.     outportb(0x02,0);
  243.     outportb(0x02,0);
  244.     outportb(0x83,aligned_physical);
  245.     outportb(0x03,(unsigned char)(len&0xFF));
  246.     outportb(0x03,(unsigned char)((len>>8)&0xFF));
  247.     outportb(0x0A,0x01);
  248.     dspwrite(0x48);
  249.     dspwrite((unsigned char)(len&0xFF));
  250.     dspwrite((unsigned char)((len>>8)&0xFF));
  251.     dspwrite(0x99);
  252. }
  253. //------------------------------------------------------------------------------
  254. unsigned short dmacount ( void )
  255. {
  256.     unsigned short x;
  257.     x=inportb(0x03);
  258.     x|=inportb(0x03)<<8;
  259.     if(x==0xFFFF) inportb(0x022E);
  260.     return(x);
  261. }
  262. //------------------------------------------------------------------------------
  263. void recording(int e,int f)
  264. {
  265.  int m,n=Triggerbuffer -e,o;
  266.  int Smax=0,Spos=0;
  267.  if(HighSpeed)sbrec2(Triggerbuffer); else sbrec(Triggerbuffer);
  268.  while(dmacount()!=0xFFFF);
  269.  for(m=0;m<n;m++)
  270.     {
  271.      o= *(aligned+m);
  272.      if(o>Smax)Smax=o,Spos=m;
  273.     }
  274.  if(f>0)for(m=0;m<e;m++)
  275.            {
  276.             o = *(aligned+Spos+m) - f;
  277.             if(o<0)o=0;
  278.             if(o>199)o=199;
  279.             SBuffer[m]=o;
  280.            }
  281.  else for(m=0;m<e;m++)SBuffer[m] = *(aligned+Spos+m) - f;
  282. }
  283.  
  284.  
  285.  
  286. void lineX00(int x,int y1,int y2)
  287. {
  288.  int dy=y1-y2;
  289.  int m=x-1,v,w=abs(dy);
  290.  if(w<2)
  291.         {
  292.          *(VGAmem+((x-1)>>3)+80*y1)&=255-(1<<(7-((x-1)&7)));
  293.          *(VGAmem+(x>>3)+80*y2)&=255-(1<<(7-(x&7)));
  294.          return;
  295.         }
  296.  if(dy<0)
  297.          {
  298.           dy=w;
  299.           for(v=y1;v<=y2;v++)
  300.              {
  301.               w=v-y1;
  302.               w /=dy;
  303.               *(VGAmem+((m+w)>>3)+80*v)&=255-(1<<(7-((m+w)&7)));
  304.              }
  305.          }
  306.  else
  307.      {
  308.       for(v=y2;v<=y1;v++)
  309.          {
  310.           w=v-y2;
  311.           w /=dy;
  312.           *(VGAmem+((x-w)>>3)+80*v)&=255-(1<<(7-((x-w)&7)));
  313.          }
  314.      }
  315. }
  316.  
  317.  
  318.  
  319. void lineX01(int x,int y1,int y2)
  320. {
  321.  int dy=y1-y2;
  322.  int m=x-1,v,w=abs(dy);
  323.  if(w<2)
  324.         {
  325.          *(VGAmem+((x-1)>>3)+80*y1)|=1<<(7-((x-1)&7));
  326.          *(VGAmem+(x>>3)+80*y2)|=1<<(7-(x&7));
  327.          return;
  328.         }
  329.  if(dy<0)
  330.          {
  331.           dy=w;
  332.           for(v=y1;v<=y2;v++)
  333.              {
  334.               w=v-y1;
  335.               w /=dy;
  336.               *(VGAmem+((m+w)>>3)+80*v)|=1<<(7-((m+w)&7));
  337.              }
  338.          }
  339.  else
  340.      {
  341.       for(v=y2;v<=y1;v++)
  342.          {
  343.           w=v-y2;
  344.           w /=dy;
  345.           *(VGAmem+((x-w)>>3)+80*v)|=1<<(7-((x-w)&7));
  346.          }
  347.      }
  348. }
  349.  
  350.  
  351.  
  352. int cool0()
  353. {
  354. unsigned t;
  355. int i,m=Triggerbuffer;
  356. Triggerbuffer=1500;
  357. Grafik(18);
  358. for